package cn.thread;

import cn.hutool.core.util.ObjectUtil;

/**
 * 消费者
 *
 * @author dunlingzi
 */
public class Consumer extends Thread {
    ProductStack ps;

    public Consumer(ProductStack ps) {
        this.ps = ps;
    }

    /**
     * 消费产品
     */
    @Override
    public void run() {
        //循环消费产品
        while (ps.isStackOpen()) {
            try {
                //消费产品  10ms消费一个
                Thread.sleep(10);
            } catch (InterruptedException e) {
                e.printStackTrace();
            }
            Product pt = ps.pop();
            if (ObjectUtil.isNotNull(pt)) {
                System.out.println("------" + Thread.currentThread().getName() + "消费了：第" + pt.getId() + "号产品");
            }
        }
        System.out.println(Thread.currentThread().getName() + "已停止消费");
    }
}